<?php 
if(!defined(__FILE__))
{
	define(__FILE__ , 1) ;
	define('RSS_DATE_FORMAT','D, j M Y H:i:s +0800');
	class Helper_Rss
	{
		var $version='2.0';
		var $xml_version='1.0';
		var $channel=array();
		var $encode='utf-8';
		var $source_encode='utf-8';
		var $css='';
		function rss($title,$url,$description=''){
			$this->channel['title']=$title;
			$this->channel['url'] = $url;
			$this->channel['description'] = $description;
			$this->channel['language']='en';
			$this->channel['ttl']='30';
			$this->channel['generator']='LTQ RSS generator v0.1';
			$this->channel['pubDate']=date(RSS_DATE_FORMAT);
			$this->channel['item']=array();
		}
		function setCSS($value)
		{
			$this->css=$value;
		}
		function set($element, $value)
		{
			$this->channel[$element]=$value;
		}
		function setImage($title,$url,$link,$width=88,$height=31, $description='')
		{
			$image = array();
			$image['title']=$title;
			$image['url']=$url;
			$image['link']=$link;
			if($width > 144) $width=144;
			$image['width']=$width;
			if($width > 400) $width=400;
			$image['height']=$height;
			$image['description']=$description;

			$this->channel['image']=$image;
		}
		function addItem($title,$link,$description,
		$pubDate='',$author='', $category='', $comments='', $source='')
		{
			$item=array();
			$item['title']=$title;
			$item['link']=$link;
			$item['description']=$description;
			if($pubDate!='')
			{
				$pubDate=date(RSS_DATE_FORMAT, $pubDate);
				$item['pubDate']=$pubDate;
			}
			if($author!='') $item['author']=$author;
			if($category!='') $item['category']=$category;
			if($comments!='') $item['comments']=$comments;
			if($source!='') $item['source']=$source;

			$this->channel['item'][]=$item;
		}
		function _encode()
		{
			$s = $this->source_encode;
			$d = $this->encode;
			if($s==$d) return;
			if(!function_exists('iconv'))
			{
				$this->encode = $this->source_encode;
				return ;
			}
			$this->__encode($this->channel);
		}
		function __encode( & $data)
		{
			if(is_string($data))
			{
				$data= iconv($this->source_encode, $this->encode, $data);
				return ;
			}
			if(is_array($data))
			{
				foreach($data as $k=>$v)
				{
					$this->__encode($data[$k]);
				}
				return ;
			}
		}

		function getDoc()
		{
			$this->_encode();
			//下面这一个分开为了避免php遇到字符串 < ? ? > 时会出错,
			$xml='<'.'?xml version="1.0" encoding="'.$this->encode.'"?'.'>';
			if($this->css!='') $xml .='<?xml-stylesheet type="text/css" href="'.$this->css.'"?>';
			$xml .='<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
<channel>'; 
			foreach($this->channel as $k=>$v)
			{
				if(is_scalar($v))
				$xml .="<{$k}>{$v}</{$k}>";
			}

			if(isset($this->channel['image']))
			{
				$xml.="<image>";
				foreach($this->channel['image'] as $k=>$v)
				$xml .="<{$k}>{$v}</{$k}>";
				$xml.="</image>";
			}

			if(count($this->channel['item'])>0)
			{
				foreach($this->channel['item'] as $item)
				{
					$xml .= "<item>";
					foreach($item as $k=>$v)
					{
						if($k=='description') $xml .="<description><![CDATA[ {$v} ]]></description>";
						else $xml .="<{$k}>{$v}</{$k}>";
					}
					$xml .= "</item>";
				}
			}

			$xml.='</channel></rss>';
			return $xml;
		}
		function SaveToFile($file)
		{
			$f = @fopen($file,'w');
			if($f)
			{
				fwrite($f, $this->getDoc());
				fclose($f);
			}else{
				exit( "<div>写入文件 {$file} 失败.请确定文件夹存在且可写入.</div>");
			}
		}

	}//end class
}//end if
?> 
